package it.pir.es10ChatWS;

import java.util.*;
import javax.jws.WebService;
import javax.xml.ws.wsaddressing.W3CEndpointReference;

@WebService
public class Chat {
	private Map<String, Connection> clients;
	
	public synchronized W3CEndpointReference getConnection(String username) {
		if(clients.containsKey(username))
			throw new RuntimeException("Utente già presente!");
		Connection connection = new Connection(this, username);
		clients.put(username, connection);
		return Connection.manager.export(connection);
	}
	
	public List<String> getUsersList() {
		return new ArrayList<String>(clients.keySet());
	}
	
	public void send(String username, String message) {
		for(Map.Entry<String, Connection> entry : clients.entrySet())
			if(!entry.getKey().equals(username))
				entry.getValue().printMessage(message);
	}
	
	/*
	public Map<String, Connection> getUsersMap() {
		return clients;
	}
	*/
}
